{ config, lib, inputs, ... }:
with lib;
{
  system.stateVersion = "25.05";

  nixpkgs.config.allowUnfree = mkDefault true;

  virtualisation.vmVariant.virtualisation = {
    cores = mkDefault 2;
    memorySize = mkDefault 4096;
  };

  environment = {
    sessionVariables.NIXPKGS_ALLOW_UNFREE = mkDefault "1";
  };

  nix = {
    # channel.enable = false;

    distributedBuilds = mkIf (config.nix.buildMachines != []) true;

    nixPath = [
      "nixpkgs=${inputs.nixpkgs}"
    ];

    registry = {
      # nix-config.flake = inputs.self;
      nixpkgs.flake = inputs.nixpkgs;
      home-manager.flake = inputs.home-manager;
      agenix.flake = inputs.agenix;
    };

    settings = {
      auto-optimise-store = true;
      builders-use-substitutes = true;
      experimental-features = [ "nix-command" "flakes" ];
      max-jobs = if config.nix.distributedBuilds then 0 else "auto";
      substituters = mkIf (!config.services.nix-serve.enable) (mkBefore [
        "https://nix.bad.net.ru/?priority=30"
        # "https://nix.notbad.dynv6.net/?priority=31"
        # "https://nix.badbayan.duckdns.org/?priority=31"
      ]);
      trusted-public-keys = mkBefore [
        "nix.bad.net.ru-1:jlTx0ty3f/d1Tb2D0Pj1O0l30/c7VpCmYLQ9Fc++xoM="
        "nix.notbad.dynv6.net-1:eN+lRGq8gGz5/PoGaAVYTnZStnEcIRHvhg+ZIn9I1TA="
        "nix.badbayan.duckdns.org-1:qI75l0GHxICLk1ftwwL1rPqcml+krIzOtZdrRs9Zrlk="
      ];
      trusted-users = [ "@wheel" ];
      warn-dirty = false;
    };

    gc = {
      automatic = true;
      dates = "monthly";
      options = "--delete-older-than 60d";
    };

    extraOptions = ''
      min-free = ${toString (1024 * 1024 * 1024)}
      max-free = ${toString (1024 * 1024 * 1024 * 4)}
    '';
  };
}
